package com.example.mypositioning

import android.content.Context
import android.util.Log
import android.widget.TextView
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption

class BaiduPositioning {
    private var mLocationClient: LocationClient? =null
    var myLocationListener:MyLocationListener?=null


    //定位SDK隐私合规接口
    fun AgreePrivacy(t:Boolean)
    {
        LocationClient.setAgreePrivacy(t)
    }

    fun init(context: Context)
    {
        try {
            mLocationClient=LocationClient(context)
            var option= LocationClientOption()
            option.locationMode= LocationClientOption.LocationMode.Battery_Saving
            option.setCoorType("bd09ll")
            option.setFirstLocType(LocationClientOption.FirstLocType.SPEED_IN_FIRST_LOC)
            option.setScanSpan(1000)

            myLocationListener=MyLocationListener()
            myLocationListener!!.mLocationClient =mLocationClient
            //myLocationListener!!.baiduPositioning=this
            mLocationClient!!.registerLocationListener(myLocationListener)//回调

            mLocationClient!!.locOption=option


        }catch (e:Exception){
            Log.e("TAG","error  "+e.toString())

        }
    }
    fun start()
    {
        mLocationClient?.start()
    }
    fun stop()
    {
        mLocationClient?.stop()
    }
    fun setTextviewtxt(textView: TextView)
    {
        myLocationListener?.textView=textView
    }
    fun setTextviewtxt1(textView: TextView)
    {
       // myLocationListener.textView=textView
    }
}

class MyLocationListener: BDAbstractLocationListener() {
    var mLocationClient:LocationClient?=null
    var textView: TextView? = null
    override fun onReceiveLocation(p0: BDLocation?) {
        //TODO("Not yet implemented")

        var latitude=p0?.latitude
        var longitude=p0?.longitude
        var radius=p0?.radius
        Log.e("TAG","经纬度  "+latitude +"   "+longitude+"   "+radius)
        Log.e("TAG","返回代码  "+p0?.locType)
        //textView1.text="经纬度  "+latitude +"   "+longitude+"   "+radius
        //textView.text="经纬度  "+latitude +"   "+longitude+"   "+radius

        if(textView!=null)
        {
            textView?.text="经纬度  "+latitude +"   "+longitude+"   "+radius
        }
        if(mLocationClient!=null)
        {
            mLocationClient!!.stop()
        }


    }

}